package com.daka.service.impl;

import com.daka.dao.AuctionTradeListDao;
import com.daka.dao.GoodsDao;
import com.daka.entity.AuctionTradeList;
import com.daka.entity.Goods;
import com.daka.service.GoodsService;
import com.daka.vo.response.GoodsVo;
import com.daka.vo.response.OffLineGoodsList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/12/28 18:38
 */
@Service("goodsService")
@SuppressWarnings("unused")
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;

    @Resource
    private AuctionTradeListDao auctionTradeListDao;

    @Override
    public int insert(Goods record) {
        return goodsDao.insert(record);
    }

    @Override
    public Goods selectById(Long id) {
        return goodsDao.selectByPrimaryKey(id);
    }

    @Override
    public Goods selectByCode(String code) {
        return goodsDao.selectByCode(code);
    }

    @Override
    public List<Goods> selectByStatement(Goods record) {
        return goodsDao.selectByStatement(record);
    }

    @Override
    public int selectByStatementCount(Goods record) {
        return goodsDao.selectByStatementCount(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Goods record) {
        return goodsDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Goods> selectAll() {
        return goodsDao.selectAll();
    }

    @Override
    public List<Goods> selectA() {
        return goodsDao.selectA();
    }

    @Override
    public List<Goods> selectB() {
        return goodsDao.selectB();
    }

    @Override
    public BigDecimal sumTotal() {
        return goodsDao.sumTotal();
    }

    /**
     * 查询需要在现场拍卖的商品列表
     */
    public OffLineGoodsList findOffLineGoods() {

        List<Goods> bGoodsList = goodsDao.selectByType(1);

        List<Goods> aOneList = goodsDao.selectByType(100);

        List<Goods> aTwoList = goodsDao.selectByType(101);

        return OffLineGoodsList.build(addPayStatus(aOneList), addPayStatus(aTwoList), addPayStatus(bGoodsList));
    }

    private List<GoodsVo> addPayStatus(List<Goods> list){

        List<GoodsVo> result = new ArrayList<>();

        for (Goods goods : list){
            AuctionTradeList param = new AuctionTradeList();
            param.setPayStatus(1);
            param.setGoods(goods.getId());
            Integer count = auctionTradeListDao.selectByStatementCount(param);
            GoodsVo goodsVo;
            if(count > 0){
                goodsVo = GoodsVo.build(goods,1);
            }else{
                goodsVo = GoodsVo.build(goods,0);
            }

            result.add(goodsVo);
        }

        return  result;
    }
}
